package es.ats.codington.web.controllers;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Places;
import es.ats.codington.festival.services.EventDAOService;
import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.PlaceDAOService;
import es.ats.codington.festival.services.VisitorService;

@Controller
public class EventController {

	@RequestMapping(value = "/show_events.htm")
	public ModelAndView showEvents(HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		ModelAndView modelAndView = new ModelAndView();
		EventSignUpService eventSignUpService = new EventSignUpService();
		EventDAOService eventDAOService = new EventDAOService();
		VisitorService visitorService = new VisitorService();
		ArrayList<Event> registeredEvents = new ArrayList<Event>();
		ArrayList<Event> unregisteredEvents = new ArrayList<Event>();
		ArrayList<Event> allEvents = new ArrayList<Event>();
		String userName = null;
		Cookie cookie= null;
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			for(int i=0; i<cookies.length; i++){
				cookie=cookies[i];
				if(cookie.getName().compareTo("user")==0)
				userName=cookie.getValue();
				
			}
		}else{
			modelAndView.setViewName("login");
			return modelAndView;
		}
		allEvents = eventDAOService.getEventListService();
		registeredEvents = eventSignUpService.eventForUserService(visitorService.getVisitorIdService(userName));
		unregisteredEvents = eventSignUpService.noEventForUserService(visitorService.getVisitorIdService(userName));
		
		modelAndView.addObject("allEvents", allEvents);
		modelAndView.addObject("registeredList", registeredEvents);
		modelAndView.addObject("unregisteredList", unregisteredEvents);
				
		modelAndView.setViewName("events");
		return modelAndView;
	}
	
	@RequestMapping(value = "/event_signup.htm")
	public ModelAndView eventSignup(HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		ModelAndView modelAndView = new ModelAndView();
		int eventId =0;
		String userName = null;
		Cookie cookie= null;
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			for(int i=0; i<cookies.length; i++){
				cookie=cookies[i];
				if(cookie.getName().compareTo("user")==0)
				userName=cookie.getValue();
				
			}
		}else{
			modelAndView.setViewName("login");
			return modelAndView;
		}
		eventId= Integer.parseInt(request.getParameter("eventId"));
		EventSignUpService eventSignUpService = new EventSignUpService();
		VisitorService visitorService = new VisitorService();
		eventSignUpService.signUpToEvent(visitorService.getVisitorIdService(userName), eventId);
	
		modelAndView.setViewName("index2");
		return modelAndView;
	}
		
	@RequestMapping(value = "/event_unsignup.htm")
	public ModelAndView eventUnsignup(HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		ModelAndView modelAndView = new ModelAndView();
		int eventId =0;
		String userName = null;
		Cookie cookie= null;
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
			for(int i=0; i<cookies.length; i++){
				cookie=cookies[i];
				if(cookie.getName().compareTo("user")==0)
				userName=cookie.getValue();
				
			}
		}else{
			modelAndView.setViewName("login");
			return modelAndView;
		}
		eventId= Integer.parseInt(request.getParameter("eventId"));
		EventSignUpService eventSignUpService = new EventSignUpService();
		VisitorService visitorService = new VisitorService();
		eventSignUpService.unsignUpToEvent(visitorService.getVisitorIdService(userName), eventId);
	
		modelAndView.setViewName("index2");
		return modelAndView;
	}
	
	@RequestMapping(value = "/inf_event.htm")
	public ModelAndView infEvent(HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		ModelAndView modelAndView = new ModelAndView();
		int eventId= Integer.parseInt(request.getParameter("eventId"));
		EventDAOService eventService = new EventDAOService();
		Event uEvent = eventService.getEventByIdService(eventId);
		modelAndView.addObject("uEvent", uEvent);
				
		modelAndView.setViewName("updateEvent");
		return modelAndView;
	}
	
	@RequestMapping(value = "/update_event.htm")
	public ModelAndView updateEvent(HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		ModelAndView modelAndView = new ModelAndView();
		EventDAOService eventDAO=new EventDAOService();
		PlaceDAOService placeDAOService=new PlaceDAOService();
		
		int eventId=Integer.parseInt(request.getParameter("eventId"));
		String eventName=request.getParameter("eventName");
		String eventDescrip=request.getParameter("eventDescrip");
		int placeId=Integer.parseInt(request.getParameter("place"));
		String duration=request.getParameter("duration");
		String eventType=request.getParameter("eventType");
		String ticket=String.valueOf(request.getParameter("ticket"));
		
		Places place;
		place=placeDAOService.getPlaceByIdService(placeId);
		Event newEvent = new Event(eventId, eventName, eventDescrip, duration, eventType, ticket, place);
		int i=eventDAO.updateEventService(newEvent);
		
		if(i==1){
			modelAndView.setViewName("index2");
			return modelAndView;
		}
		else {
			modelAndView.setViewName("updateEvent");
			return modelAndView;
		}
	}
	
	@RequestMapping(value = "/event_registation.htm")
	public ModelAndView eventRegistration(HttpServletRequest request, HttpServletResponse response) throws IOException {

		ModelAndView modelAndView = new ModelAndView();
		EventDAOService eventDAO=new EventDAOService();
		PlaceDAOService placeDAOService=new PlaceDAOService();
		
		String eventName=request.getParameter("eventName");
		String eventDescrip=request.getParameter("eventDescrip");
		int placeId=Integer.parseInt(request.getParameter("place"));
		String duration=request.getParameter("duration");
		String eventType=request.getParameter("eventType");
		String ticket=request.getParameter("ticket");

		Places place;
		place=placeDAOService.getPlaceByIdService(placeId);
		Event event=new Event(eventName, eventDescrip,duration,eventType,ticket,place);
		int i=eventDAO.insertEventService(event);
		
		if(i==1){
			modelAndView.setViewName("index2");
			return modelAndView;
		}
		else {
			modelAndView.setViewName("eventRegistration");
			return modelAndView;
		}
	}
	
	@RequestMapping(value = "/delete_event.htm")
	public ModelAndView deleteEvent(HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		ModelAndView modelAndView = new ModelAndView();
		int eventId= Integer.parseInt(request.getParameter("eventId"));
		EventDAOService eventDAOService = new EventDAOService();
		eventDAOService.deleteEventService(eventId);
		modelAndView.setViewName("index2");
		return modelAndView;
	}
}
